package leetcode_bin_tree_test;

import bin_tree.TreeNode;

/**
 * 剑指 Offer 55 - II. 平衡二叉树
 */
public class IsBalanced2 {
    public boolean isBalanced(TreeNode root) {
        return height(root) >= 0;
    }

    private int height(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int left = height(root.left);
        int right = height(root.right);
        if (left == -1 || right == -1 || Math.abs(left - right) > 1) {
            return -1;
        }
        return 1 + Math.max(left, right);
    }
}
